package com.yayan.xh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yayan.xh.bean.EnterBean;
import com.yayan.xh.bean.MatchBean;
import com.yayan.xh.bean.OperateBean;
import com.yayan.xh.service.IBannerService;
import com.yayan.xh.service.IEnterService;
import com.yayan.xh.service.IIntegralService;
import com.yayan.xh.service.IMatchService;
import com.yayan.xh.service.IMemberService;
import com.yayan.xh.service.INewsService;
import com.yayan.xh.service.IOperateService;
import com.yayan.xh.service.IOrderService;
import com.yayan.xh.service.IReviewService;

@Controller
@RequestMapping(value="/front")
public class FrontController {
	
	@Autowired
	private IMatchService matchService;
	
	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private IMemberService memberService;
	
	@Autowired
	private IReviewService reviewService;
	
	@Autowired
	private IEnterService enterService;
	
	@Autowired
	private IIntegralService integralService;
	
	@Autowired
	private INewsService newsService;
	
	@Autowired
	private IBannerService bannerService;
	
	@Autowired
	private IOperateService operateService;
	
	/**
	 * 微信用户授权登陆接口
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/login.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(@RequestBody Map<String, String> map){
		Map<String, Object> result = new HashMap<String, Object>();		
		Map<String, Object> userMap = memberService.frontLogin(map);
		if(null != userMap) {
			result.put("status", 1);
			result.put("message", "success");
			result.put("list", userMap);
		} else {
			result.put("status", 0);
			result.put("message", "登录失败");
		}
		return result;
	}
	
	/**
	 * 保存用户信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/saveUser.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveUser(@RequestBody Map<String, String> map){
		Map<String, Object> result = new HashMap<String, Object>();		
		if(memberService.saveUser(map)) {
			result.put("status", 1);
			result.put("message", "success");
		} else {
			result.put("status", 0);
			result.put("message", "保存失败");
		}
		return result;
	}
	
	/**
	 * 获取赛事信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getMatch.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getMatch(@RequestBody Map<String, String> map){		
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> list = matchService.getMatchList(map);
		if(null != list && list.size() > 0) {
			if(list.size() == 1) { 
				EnterBean enterBean = enterService.getFrontEnterById(map);
				if(null != enterBean) {
					result.put("enterflag", "true");
				} else {
					result.put("enterflag", "false");
				}
			} else {
				result.put("enterflag", null);
			}
			result.put("status", 1);
			result.put("message", "success");
			result.put("matchList", list);
		} else {
			result.put("status", 0);
			result.put("message", "没有查到相关数据");
		}
		return result;
	}
	
	/**
	 * 获取赛事回顾信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getReview.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getReview(@RequestBody Map<String, String> map){		
		Map<String, Object> result = new HashMap<String, Object>();
		if(null == map.get("curPage") || "".equals("curPage")) {
			map.put("curPage", "1");
		}
		//获得总页数
		Integer totalPage = reviewService.getReviewPageCount(map);
		//获得分页数据
		List<Map<String, Object>> list = reviewService.getReviewPage(map);
		if(null != list && list.size() > 0) {
			result.put("status", 1);
			result.put("message", "success");
			result.put("totalPage", totalPage);
			result.put("curPage", map.get("curPage"));
			result.put("matchList", list);
		} else {
			result.put("status", 0);
			result.put("message", "查询失败");
		}
		return result;
	}
	
	
	/**
	 * 生成订单
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/saveOrder.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> saveOrder(@RequestBody Map<String, String> map) {
		Map<String, String> result = new HashMap<String, String>();
		EnterBean enterBean = enterService.getFrontEnterById(map);
		if(null != enterBean) {
			return result;
		}
 		result = orderService.saveOrder(map);
		return result;
	}
	
	/**
	 * 查询未签到信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getEnter.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getEnter(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> list = enterService.getFrontEnter(map);
		if(null != list && list.size() > 0) {
			result.put("status", 1);
			result.put("message", "success");
			result.put("matchList", list);
		} else {
			result.put("status", 0);
			result.put("message", "查询失败");
		}
		return result;
	}
	
	/**
	 * 查询参与信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getJoin.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getJoin(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> list = enterService.getFrontJoin(map);
		if(null != list && list.size() > 0) {
			result.put("status", 1);
			result.put("message", "success");
			result.put("matchList", list);
		} else {
			result.put("status", 0);
			result.put("message", "查询失败");
		}
		return result;
	}
	
	/**
	 * 签到
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/signCode.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> signCode(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		EnterBean bean = enterService.getFrontEnterById(map);
		if(bean.getE_sginstatus() == 1) {
			result.put("status", 0);
			result.put("message", "用户已签到");
		} else {
			if(enterService.signCode(map)) {
				result.put("status", 1);
				result.put("message", "success");
				MatchBean matchBean = matchService.getMatchByMatchIdBack(map);
				result.put("matchName", matchBean.getM_name());
				result.put("matchDate", matchBean.getM_stardatetime());
				result.put("matchAddress", matchBean.getM_address());
			} else {
				result.put("status", 0);
				result.put("message", "签到失败");
			}
		}
		
		
		return result;
	}
	
	/**
	 * 查询积分
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getIntegral.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getIntegral(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> list = integralService.getFrontIntegral(map);
		if(null != list && list.size() > 0) {
			result.put("status", 1);
			result.put("message", "success");
			result.put("list", list);
		} else {
			result.put("status", 0);
			result.put("message", "查询失败");
		}
		return result;
	}
	
	/**
	 * 查询资讯信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getNews.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getNews(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(null == map.get("curPage") || "".equals("curPage")) {
			map.put("curPage", "1");
		}
		//获得总页数
		Integer totalPage = newsService.getNewsPageCount(map);
		//获得分页数据
		List<Map<String, Object>> list = newsService.getNewsPage(map);
		if(null != list && list.size() > 0) {
			result.put("status", 1);
			result.put("message", "success");
			result.put("totalPage", totalPage);
			result.put("curPage", map.get("curPage"));
			result.put("matchList", list);
		} else {
			result.put("status", 0);
			result.put("message", "查询失败");
		}
		return result;
	}
	
	/**
	 * 查询banner
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getBanner.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getBanner(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> list = bannerService.getBannerList(map);
		if(null != list && list.size() > 0) {
			result.put("status", 1);
			result.put("message", "success");
			result.put("bannerList", list);
		} else {
			result.put("status", 0);
			result.put("message", "查询失败");
		}
		return result;
	}
	
	/**
	 * 后台登录校验
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/backlogin.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> backlogin(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		OperateBean bean = operateService.adminLogin(map);
		if(null != bean){		
			result.put("status", 1);
			result.put("message", "success");
		} else {
			result.put("status", 0);
			result.put("message", "用户名或账号错误");
		}
		return result;
	}
	
	/**
	 * 获取用户信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getUser.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getUser(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		Map<String, Object> memberInfo = memberService.getUser(map);
		if(null != memberInfo && !memberInfo.isEmpty()){		
			result.put("status", 1);
			result.put("message", "success");
			result.put("member", memberInfo);
		} else {
			result.put("status", 0);
			result.put("message", "查询失败");
		}
		return result;
	}
	
	/**
	 * 修改标签
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/modifyLabel.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyLabel(@RequestBody Map<String, String> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(memberService.modifyUserTabel(map)){		
			result.put("status", 1);
			result.put("message", "success");
		} else {
			result.put("status", 0);
			result.put("message", "修改失败");
		}
		return result;
	}
}
